package com.brush.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brush.common.constant.CacheConstants;
import com.brush.common.exception.ServiceException;
import com.brush.common.exception.database.PrimaryKeyException;
import com.brush.common.exception.database.RecordNotFoundException;
import com.brush.common.utils.MessageUtils;
import com.brush.system.domain.SysPost;
import com.brush.system.domain.req.syspost.SysPostAddReq;
import com.brush.system.domain.req.syspost.SysPostEditReq;
import com.brush.system.domain.req.syspost.SysPostQueryReq;
import com.brush.system.domain.resp.syspostresp.SysPostResp;
import com.brush.system.mapper.SysPostMapper;
import com.brush.system.service.SysPostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 18494
 * @description 针对表【sys_post(岗位信息表)】的数据库操作Service实现
 * @createDate 2024-08-19 10:28:03
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost>
        implements SysPostService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<SysPostResp> paginationQuery(SysPostQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public boolean add(SysPostAddReq req) {
        // 验证postName和postCode是否已经存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostName, req.getPostName()))) {
            throw new ServiceException(MessageUtils.message("syspost.postName.exists"));
        }
        if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostCode, req.getPostCode()))) {
            throw new ServiceException(MessageUtils.message("syspost.postCode.exists"));
        }

        SysPost newEntity = new SysPost();
        BeanUtils.copyProperties(req, newEntity);

        boolean save = this.save(newEntity);
        if (save) {
            // 清空缓存
            redisTemplate.opsForValue().set(CacheConstants.SYS_CONFIG_KEY + req.getPostCode(), newEntity);
        }
        return save;
    }

    @Override
    public SysPostResp findById(Long id) {
        if (Objects.isNull(id)) {
            throw new PrimaryKeyException();
        }

        // 根据岗位ID查询
        SysPost db = this.getById(id);
        // 数据库找不到数据
        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }

        // 封装成返回对象
        SysPostResp resp = new SysPostResp();
        BeanUtils.copyProperties(db, resp);

        return resp;
    }

    @Override
    public boolean edit(SysPostEditReq req) {
        // 根据岗位ID查询
        SysPost db = this.getById(req.getPostId());
        // 数据库找不到数据
        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }

        // 验证postName和postCode是否已经存在
        if (!req.getPostName().equals(db.getPostName()) || !req.getPostCode().equals(db.getPostCode())) {
            // 判断postName或postCode是否已经被使用
            if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostName, req.getPostName()))) {
                throw new ServiceException(MessageUtils.message("syspost.postName.exists"));
            }
            if (this.baseMapper.exists(new LambdaQueryWrapper<SysPost>().eq(SysPost::getPostCode, req.getPostCode()))) {
                throw new ServiceException(MessageUtils.message("syspost.postCode.exists"));
            }
        }

        // 覆盖数据库对象的值
        BeanUtils.copyProperties(req, db);
        boolean result = this.updateById(db);
        if (result) {
            // 清空缓存
            redisTemplate.opsForValue().set(CacheConstants.SYS_CONFIG_KEY + db.getPostCode(), db);
        }
        return result;
    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return this.baseMapper.selectPostListByUserId(userId);
    }
}




